package com.blackline.auto.Report;

import static org.junit.Assert.fail;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

import com.blackline.auto.common.CommonMethods;
import com.blackline.declareUI.UI;
import com.blackline.parameters.Parameters;
import com.blackline.parameters.TestData;

/**
 * @author Allen
 * @version May 24, 2013
 * @Updated by Joie on Aug 08,2013
 *          <p>
 *          This is to run all 17 standard reports with all 14 roles.
 */


public class RunUPSReportsWithAllRoles
{

	private WebDriver driver;
	private StringBuffer verificationErrors = new StringBuffer();
	private static Logger logger = Logger.getLogger(RunUPSReportsWithAllRoles.class);
	UI UI = new UI();
	Parameters parameters;

	int sleepTime = 2000;

	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-kk-mm-ss");
	Date date = new Date();
	String dateString = sdf.format(date);
    //Log in User as System Admin
	TestData testdata = new TestData("TestData/RunStdReportsWithAllRoles.xml");

	String t2Test2 = testdata.getInput("t2Test2");
	String testStarter = testdata.getInput("testStarter");
	String testInternal2 = testdata.getInput("testInternal2");
	String user = testdata.getInput("user");
	String password = testdata.getInput("password");
    //Get Roles
	String[] roles =CommonMethods.getRunParameter("TestData/BL_AllRoles.xml","Role");
    //Get UPS reports which need run
	String[] UPSReports =CommonMethods.getRunParameter("TestData/BL_UPSReports.xml", "UPSRpt");
	

	String desc_string_for_Account_Reconciliation_Summary = "category totals";
	String desc_string_for_Certification_Status_By_Entity = "Summary report";

	@Before
	public void setUp() throws Exception
	{
		FirefoxProfile profile = new FirefoxProfile();
		profile.setEnableNativeEvents(true);
		parameters = new Parameters("parameters.xml");

		driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		UI.declareAllUI(driver);
	}

	
	
	@Test
	public void RunWithAllRoles3WXBOSoft() throws Exception
	{
		// Log into web site.
		logger.info("You've selected site: " + t2Test2 + " with: " + user);
		UI.LogonPage.Login(driver, t2Test2, user, password);
		
		// Start run the report with different roles.
		runUPSReportsWithSpecificRole(1, t2Test2);
		runUPSReportsWithSpecificRole(2, t2Test2);
		runUPSReportsWithSpecificRole(3, t2Test2);
		runUPSReportsWithSpecificRole(4, t2Test2);
		runUPSReportsWithSpecificRole(5, t2Test2);
		runUPSReportsWithSpecificRole(6, t2Test2);
		runUPSReportsWithSpecificRole(7, t2Test2);
		runUPSReportsWithSpecificRole(8, t2Test2);
		runUPSReportsWithSpecificRole(9, t2Test2);
		runUPSReportsWithSpecificRole(10, t2Test2);
		runUPSReportsWithSpecificRole(11, t2Test2);
		runUPSReportsWithSpecificRole(12, t2Test2);
		runUPSReportsWithSpecificRole(13, t2Test2);
		runUPSReportsWithSpecificRole(14, t2Test2);
		runUPSReportsWithSpecificRole(15, t2Test2);
		
	}

	
	@After
	public void tearDown() throws Exception
	{
		 CommonMethods.takeScreenShot(this.getClass(), driver);
		 logger.info("===========================================");
		// driver.close();
		// driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString))
		{
			fail(verificationErrorString);
		}
	}

	/**
	 * This is to run the UPS reports with specific roles
	 * 
	 * @param roleName
	 *            The name of the role to run the reports
	 */
	private void runUPSReportsWithSpecificRole(int sequence, String site) throws Exception
	{
		// Switch to different roles
		UI.SessionMenu.switchRoleBaseOnSequence(sequence);

		// Go to Enhanced Reporting page
		Thread.sleep(sleepTime);
		driver.get(site);
		Thread.sleep(sleepTime);

		// ======== Go to Accounts node========
		logger.info("=====UPS Node=====");
		UI.Reporting.UPS_Reports_nodeInLeftTree.click();
		Thread.sleep(sleepTime);
		UI.Reporting.AddIcon_UPSReports.click();
		Thread.sleep(sleepTime);
		UI.Reporting.UPS_nodeInLeftTree.click();
		Thread.sleep(sleepTime);
		
		
		// Run Report
		logger.info("Going to run: " + UPSReports[0]);
		UI.Reporting.Name_text_field.clear();
		UI.Reporting.Description_text_field.clear();
		UI.Reporting.Name_text_field.clear();
		
		UI.Reporting.Name_text_field.sendKeys(UPSReports[0]);
		UI.Reporting.Name_text_field.sendKeys(Keys.RETURN);
		Thread.sleep(sleepTime);

		UI.Reporting.Run_linkOfSearchedResult.click();
		Thread.sleep(sleepTime);
		UI.Reporting.Run_Report_button.click();
		Thread.sleep(sleepTime);
		UI.Reporting.OK_button.click();
		Thread.sleep(sleepTime);

	
		logger.info("==========Current time is: " + dateString + "; Role " + sequence + " done.==========");

	}


}
